﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EmoneyWeiXinEntity;
using EmoneyWeiXinCommon;
namespace EmoneyWeiXinDAL
{
    public class NewsReportDal
    {
        public static List<NewsReportEntity> getNewsReportList(int pindex, int psize, out int total, string memutype, string pubdate1, string pubdate2, string title, int weixinid)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@pindex",pindex),
            new SqlParameter("@psize", psize),
            new SqlParameter("@total",0),
            new SqlParameter("@memutype",memutype),
            new SqlParameter("@pubdate1",pubdate1),
            new SqlParameter("@pubdate2",pubdate2),
            new SqlParameter("@title",title),
            new SqlParameter("@weixinid",weixinid)
            };
            parameters[2].Direction = ParameterDirection.Output;
            List<NewsReportEntity> result = SQLHelper.GetProcedureDataByColumnsLst<NewsReportEntity>(Config.EmoneyWeiXin, "sp_newsReport_Select", parameters);
            total = Convert.ToInt32(parameters[2].Value);
            return result;
        }
        public static List<NewsReportEntity> getNewsReportList2(int pindex, int psize, out int total, string memutype, string pubdate1, string pubdate2, string title, int weixinid)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@pindex",pindex),
            new SqlParameter("@psize", psize),
            new SqlParameter("@total",0),
            new SqlParameter("@memutype",memutype),
            new SqlParameter("@pubdate1",pubdate1),
            new SqlParameter("@pubdate2",pubdate2),
            new SqlParameter("@title",title),
            new SqlParameter("@weixinid",weixinid)
            };
            parameters[2].Direction = ParameterDirection.Output;
            List<NewsReportEntity> result = SQLHelper.GetProcedureDataByColumnsLst<NewsReportEntity>(Config.EmoneyWeiXin, "sp_newsReport_Select2", parameters);
            total = Convert.ToInt32(parameters[2].Value);
            return result;
        }
        public static List<NewsReportEntity> getNewsReportListByID(int id)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@ID", id)
            };
            List<NewsReportEntity> result = SQLHelper.GetProcedureDataByColumnsLst<NewsReportEntity>(Config.EmoneyWeiXin, "sp_newsReport_SelectByID", parameters);
            return result;
        }
        public static int UpdateNewsReport(NewsReportEntity obj)
        {
            SqlParameter[] parameters = new SqlParameter[] {
            new SqlParameter("@ID" , obj.ID),				
            new SqlParameter("@MemuType" , obj.MemuType),				
            new SqlParameter("@title", obj.title),				
            new SqlParameter("@Author" , obj.Author),				
            new SqlParameter("@pubDate", obj.pubDate),				
            new SqlParameter("@Stock",obj.Stock),				
            new SqlParameter("@keyword", obj.keyword),				
            new SqlParameter("@Summary", obj.Summary),				
            new SqlParameter("@Content", obj.Content),				
            new SqlParameter("@PubStatus", obj.PubStatus),				
            new SqlParameter("@Step", obj.Step),				
            new SqlParameter("@OutsideUrl", obj.OutsideUrl),				
            new SqlParameter("@ImageUrl", obj.ImageUrl),				
            new SqlParameter("@Nature", obj.Nature),				
            new SqlParameter("@isupdate", obj.isupdate),				
            new SqlParameter("@isvalid", obj.isvalid),				
            new SqlParameter("@createtime", obj.createtime),				
            new SqlParameter("@updatetime", obj.updatetime),				
            new SqlParameter("@remark", obj.remark),				
            new SqlParameter("@newguid", obj.newguid),				
            new SqlParameter("@PubMethod", obj.PubMethod),				
            new SqlParameter("@source", obj.source),
            new SqlParameter("@weixinid", obj.weixinid)
            };
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_newsReport_Update", parameters);
            return result;
        }
        public static int UpdateValidNewsReport(int id, int ivalid)
        {
            SqlParameter[] parameters = new SqlParameter[] {  
            new SqlParameter("@ID", id),
            new SqlParameter("@isvalid", ivalid)
            };
            int result = SQLHelper.ExecuteNonQuery(Config.EmoneyWeiXin, CommandType.StoredProcedure, "sp_newsReport_Delete", parameters);
            return result;
        }

        public DateTime getList_date(int typeid)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@typeid", typeid) };
            return Convert.ToDateTime(SQLHelper.ExecuteScalar(Config.EmoneyWeiXin, CommandType.StoredProcedure, "[sp_news_list0_getdate]", parameters));
        }

        public IList<NewsReportEntity> getNewsForClientDayByDay(string meid, string day, int pre)
        {
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@typeID", meid),
            new SqlParameter("@day", day),
            new SqlParameter("@pre", pre)};
            return SQLHelper.GetProcedureDataByColumnsLst<NewsReportEntity>(Config.EmoneyWeiXin, "sp_News_SelectForClientDayByDay", parameters);
        }


        public IList<NewsReportEntity> getNewsForClient2(string meid, bool latestDay, int size, int index)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@typeID", meid),
            new SqlParameter("@day", latestDay ? 1 : 0),
            new SqlParameter("@size", size),
            new SqlParameter("@index", index)};
            //return ExecuteDataSet(ConnectionManager.Default, cmd);
            return SQLHelper.GetProcedureDataByColumnsLst<NewsReportEntity>(Config.EmoneyWeiXin, "sp_News_SelectForClient", parameters);
        }

        public NewsReportEntity GetLastVipNewsReport()
        {
            string sql = "SELECT top 1 * FROM newsReport with(nolock) where MemuType='" + ConstName.VipZixunName + "' AND weixinId=8 ORDER BY createtime DESC";
            return SQLHelper.GetDataByColumns<NewsReportEntity>(Config.EmoneyWeiXin, sql, null);
            // return null;
        }
    }
}
